﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using CANLib;
using dotnetCHARTING;
using dotnetCHARTING.WinForms;

namespace CAN_Monitor
{
    public partial class frmDiagram : Form
    {
        #region ctor

        public frmDiagram(frmMain parent)
        {
            InitializeComponent();
            mParentMainWindow = parent;

            this.timer1.Start();
        }

        #endregion

        #region Members

        private frmMain mParentMainWindow = null;
        private List<CANPacket> mChangeList = new List<CANPacket>();

        #endregion

        #region Properites

        #endregion

        #region Methods

        /// <summary>
        /// Updates the pakets in the diagram
        /// </summary>
        public void UpdatePackets(CANPacket[] packets)
        {
            //Add the new packets to the change list
            this.mChangeList.AddRange(packets);
        }

        #endregion

        #region Events

        //----------------------------------------------------------------------------------------

        private void btnSetPreferences_Click(object sender, EventArgs e)
        {
            mChart.SeriesCollection.Clear();

            SeriesCollection collection = new SeriesCollection();

            //TODO: Setup diagram
            foreach (object o in clbPakets.CheckedItems)
            {
                Series s = new Series(o.ToString());

                collection.Add(s);
            }//foreach


            //Create data objects for the series
            List<double>[] data = new List<double>[collection.Count];

            //Create lists for saving data
            for (int i = 0; i < collection.Count; i++)
            {
                data[i] = new List<double>();
            }

            //Fill lists with data
            foreach (CANPacket packet in mParentMainWindow.Packets)
            {
                for (int i = 0; i < collection.Count; i++)
                {
                    if (collection[i].Name == packet.ID.ToString())
                    {
                        //Data is relevant
                        data[i].Add(BitConverter.ToDouble(packet.Data, 0));
                    }
                }
            }//foreach

            //Add data to chart
            //mChart.SeriesCollection.Clear();
            //mChart.SeriesCollection.Add(collection);
            mChart.ChartArea = new ChartArea(collection);

            switch (cmbDiagType.SelectedIndex)
            {
                case 0:
                    //Value chart
                    mChart.Type = ChartType.Financial;
                    break;
            }
        }

        //----------------------------------------------------------------------------------------

        private void cmbDiagType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbDiagType.SelectedIndex == 1)
            {
                cmbTimeSpan.Enabled = true;
            }
            else
            {
                cmbTimeSpan.Enabled = false;
            }
        }

        //----------------------------------------------------------------------------------------

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.mChangeList.Count != 0)
            {
                //Add each type of packet to the list
                foreach (CANPacket packet in this.mChangeList)
                {
                    if (!clbPakets.Items.Contains(packet.ID))
                    {
                        clbPakets.Items.Add(packet.ID);
                    }
                }
            }
        }

        //----------------------------------------------------------------------------------------

        #endregion


    }
}
